<%-- 
    Document   : rule
    Created on : 2013-5-10, 10:18:49
    Author     : piaohao
--%>

<%@page import="org.piaohao.util.time.TimeUtil"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    String empName = (String) request.getSession().getAttribute("empName");
    String orgId = (String) request.getSession().getAttribute("orgId");
%>
<!DOCTYPE html>
<html>
    <head>
        <base href="<%= basePath%>"/>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <%@include file="../main/common.jsp" %>
        <title>规章制度</title>
        <script type="text/javascript">
            Ext.onReady(function() {
                var ruleForm = Ext.create("Ext.form.Panel", {
                    url: "rule/add",
                    bodyPadding: 10,
                    layout: {
                        type: "table",
                        columns: 3
                    },
                    defaults: {
                        labelAlign: "right"
                    },
                    defaultType: "textfield",
                    items: [
                        {fieldLabel: "文件名称", name: "rule.name", allowBlank: false},
                        {fieldLabel: "文号", name: "rule.number", allowBlank: false},
                        {
                            xtype: "combo",
                            fieldLabel: '制度类别',
                            labelAlign: "right",
                            editable: false,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: [
                                    {text: "安全保障", value: "安全保障"},
                                    {text: "过程管理", value: "过程管理"}
                                ]
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "rule.type",
                            value: "",
                            listeners: {
                                select: function(combo, record) {
                                    var val = record[0].data.value;
                                    var store = Ext.getCmp("proCombo").getStore();
                                    if (val === "安全保障") {
                                        store.loadData([
                                            {text: "机构管理", value: "机构管理"},
                                            {text: "文件管理", value: "文件管理"}
                                        ]);
                                    } else if (val === "过程管理") {
                                        store.loadData([
                                            {text: "驾驶员档案", value: "驾驶员档案"},
                                            {text: "车辆档案", value: "车辆档案"},
                                            {text: "线路档案", value: "线路档案"}
                                        ]);
                                    }
                                }
                            }
                        },
                        {
                            id: "proCombo",
                            xtype: "combo",
                            fieldLabel: '过程名称',
                            labelAlign: "right",
                            editable: false,
                            store: Ext.create('Ext.data.Store', {
                                fields: ['text', 'value'],
                                data: []
                            }),
                            queryMode: 'local',
                            displayField: 'text',
                            valueField: 'value',
                            name: "rule.process_name",
                            value: ""
                        },
                        {fieldLabel: "发布日期", name: "publish_date", allowBlank: false, xtype: "datefield", editable: false, format: "Y-m-d"},
                        {fieldLabel: "登记人", name: "rule.reg_person", readOnly: true},
                        {fieldLabel: "登记日期", name: "reg_date", colspan: 3, allowBlank: false, readOnly: true},
                        {xtype: "htmleditor", fieldLabel: "文件内容", name: "rule.content", colspan: 3, width: 1000, height: 300},
                        {name: "rule.id", xtype: "hiddenfield"},
                        {name: "rule.org_id", xtype: "hiddenfield"}
                    ],
                    buttons: [{
                            text: '重置',
                            iconCls: "refresh",
                            handler: function() {
                                this.up('form').getForm().reset();
                            }
                        }, {
                            text: '提交',
                            formBind: true, //only enabled once the form is valid
                            disabled: true,
                            iconCls: "save",
                            handler: function() {
                                var form = this.up('form').getForm();
                                if (form.isValid()) {
                                    form.submit({
                                        success: function(form, action) {
                                            var json = action.result;
                                            Ext.Msg.alert('提示', json.msg, function() {
                                                ruleWindow.close();
                                                ruleStore.load();
                                            });
                                        },
                                        failure: function(form, action) {
                                            Ext.Msg.alert('提示', action.result.msg);
                                        }
                                    });
                                }
                            }
                        }, {
                            text: "关闭",
                            iconCls: "close",
                            handler: function() {
                                ruleWindow.close();
                            }
                        }]
                });
                var ruleWindow = Ext.create("Ext.window.Window", {
                    title: "规章制度",
                    maximized: true,
                    maximizable: true,
                    closeAction: "hide",
//                    layout: "fit",
                    overflowY: "auto",
                    items: [ruleForm]
                });
                var ruleStore = Ext.create('Ext.data.Store', {
                    fields: ["id", "name", "number", "type", "process_name", "publish_date", "reg_person", "reg_date", "orgName"],
                    pageSize: 15,
                    proxy: {
                        type: 'ajax',
                        url: 'rule/list',
                        reader: {
                            type: 'json',
                            root: 'data'
                        }
                    },
                    autoLoad: true
                });
                var ruleGrid = Ext.create("Ext.grid.Panel", {
                    title: "规章制度",
                    columnLines: true,
                    frame: true,
                    selType: 'checkboxmodel', // 设置选择模式 
                    multiSelect: true, // 允许多选
                    forceFit: true,
                    columns: [
                        {header: '主键', dataIndex: 'id', hidden: true},
                        {header: '文件名称', dataIndex: 'name', width: 120},
                        {header: '文号', dataIndex: 'number', width: 100},
                        {header: '制度类别', dataIndex: 'type', width: 100},
                        {header: '过程名称', dataIndex: 'process_name', width: 100},
                        {header: '登记人', dataIndex: 'reg_person', width: 100},
                        {header: '登记日期', dataIndex: 'reg_date', width: 100},
                        {header: '所属机构', dataIndex: 'orgName', width: 100}

                    ],
                    store: ruleStore,
                    tbar: [
                        {xtype: "button", text: "查看", iconCls: "search",
                            handler: function() {
                                var sel = ruleGrid.getSelectionModel().getSelection();
                                if (sel.length !== 1) {
                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                    return;
                                }
                                var record = sel[0].data;
                                var detailWindow = Ext.create("Ext.window.Window", {
                                    title: "规章制度",
                                    maximized: true,
                                    maximizable: true,
                                    closeAction: "hide",
                                    modal: true,
                                    layout: "fit",
                                    html: "<iframe id='detailFrame' src='rule/showDetail/" + record.id + "' width='100%' height='100%'></iframe>",
                                    buttons: [
                                        {text: "打印", iconCls: "print",
                                            handler: function() {
                                                Ext.get("detailFrame").dom.contentWindow.print();
                                            }},
                                        {text: "关闭", iconCls: "close",
                                            handler: function() {
                                                detailWindow.close();
                                            }}
                                    ]
                                });
                                detailWindow.show();
                            }},
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "登记", iconCls: "add",
                            handler: function() {
                                ruleForm.getForm().reset();
                                ruleForm.getForm().url = "rule/add";
                                ruleForm.getForm().setValues({
                                    "rule.reg_person": "${empName}",
                                    "reg_date": "<%= TimeUtil.getNow("-")%>",
                                    "rule.org_id": "${orgId}"
                                });
                                ruleWindow.show();
                            }},
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "变更", iconCls: "edit",
                            handler: function() {
                                var sel = ruleGrid.getSelectionModel().getSelection();
                                if (sel.length !== 1) {
                                    Ext.Msg.alert("提示", "请选择1条且仅有1条记录！");
                                    return;
                                }
                                var record = sel[0];
                                ruleForm.getForm().url = "rule/update";
                                var tag = true;
                                Ext.Ajax.request({
                                    url: "rule/getRuleById",
                                    params: {
                                        ruleId: record.get("id")
                                    },
                                    success: function(response) {
                                        var json = Ext.JSON.decode(response.responseText);
                                        ruleForm.getForm().setValues({
                                            "rule.id": json.rule.id,
                                            "rule.name": json.rule.name,
                                            "rule.number": json.rule.number,
                                            "rule.process_name": json.rule.process_name,
                                            "rule.type": json.rule.type,
                                            "rule.reg_person": json.rule.reg_person,
                                            "reg_date": json.rule.reg_date.substring(0, 10),
                                            "publish_date": json.rule.publish_date.substring(0, 10),
                                            "rule.content": json.rule.content
                                        });
                                    },
                                    failure: function(response) {
                                        var json = Ext.JSON.decode(response.responseText);
                                        Ext.Msg.alert("提示", json.msg, function() {
                                            tag = false;
                                        });
                                    }
                                });
                                if (!tag) {
                                    Ext.Msg.alert("提示", "获取账户信息失败！");
                                    return;
                                }
                                ruleWindow.show();
                            }
                        },
                        {xtype: 'tbseparator'},
                        {xtype: "button", text: "删除", iconCls: "remove",
                            handler: function() {
                                Ext.Msg.confirm("提示", "删除不可恢复，您确定删除吗？", function(id) {
                                    if (id === "yes") {
                                        var mask = new Ext.LoadMask(ruleGrid, {msg: "正在删除..."});
                                        mask.show();
                                        var sel = ruleGrid.getSelectionModel().getSelection();
                                        var ids = [];
                                        for (i = 0; i < sel.length; i++) {
                                            ids.push(sel[i].data.id);
                                        }
                                        Ext.Ajax.request({
                                            url: "rule/delete",
                                            params: {
                                                ids: ids
                                            },
                                            success: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    ruleGrid.getStore().remove(sel);
                                                    mask.hide();
                                                });
                                            },
                                            failure: function(response) {
                                                var json = Ext.JSON.decode(response.responseText);
                                                Ext.Msg.alert("提示", json.msg, function() {
                                                    mask.hide();
                                                });
                                            }
                                        });
                                    }
                                });
                            }
                        }
                    ],
                    bbar: Ext.create('Ext.PagingToolbar', {
                        store: ruleStore,
                        displayInfo: true
                    })
                });
                var viewport = Ext.create("Ext.Viewport", {
                    layout: {type: "border"},
                    items: [
                        {region: "center", layout: "fit", items: [ruleGrid]}
                    ]
                });
            });
        </script>
    </head>
    <body>
    </body>
</html>
